//
//  Date.m
//  Remind
//
//  Created by macZF on 2017/11/3.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "Date.h"

@implementation Date

+ (NSDate *)strToDate:(NSString *)str Formatter:(NSString *)format{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate * date = [formatter dateFromString:str];
    return date;
}

+ (NSString *)dateToStr:(NSDate *)date Formatter:(NSString *)format
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString * dateString = [formatter stringFromDate:date];
    return [dateString copy];
}

+ (NSTimeInterval)BetweenDateAndTime:(NSDate *)startDate toDate:(NSDate *)endDate{
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}

//计算两个日期之间相差几天
+ (NSInteger)betweenTheDays:(NSString *)startDate toDate:(NSString *)endDate{
    NSString *startStr = [[startDate substringToIndex:10] stringByAppendingString:@" 00:00:00"];
    NSString *endStr = [[endDate substringToIndex:10] stringByAppendingString:@" 00:00:00"];
   
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *start = [dateFormatter dateFromString:startStr];
    NSDate *end = [dateFormatter dateFromString:endStr];
    
    NSTimeInterval time = [end timeIntervalSinceDate:start];
    
    NSInteger days = time / 86400;
    return days;
}

//传入今天的时间，返回明天的时间
+ (NSString *)GetTomorrowDay:(NSDate *)aDate Formatter:(NSString *)formatter{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]+1)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    return [self dateToStr:beginningOfWeek Formatter:formatter];
}


@end
